<%
# Copyright (C) 2011 - present Instructure, Inc.
#
# This file is part of Canvas.
#
# Canvas is free software: you can redistribute it and/or modify it under
# the terms of the GNU Affero General Public License as published by the Free
# Software Foundation, version 3 of the License.
#
# Canvas is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
# details.
#
# You should have received a copy of the GNU Affero General Public License along
# with this program. If not, see <http://www.gnu.org/licenses/>.
%>

<% content_for :page_title do %><%= t :title, "Jobs CP" %><% end %>
<% content_for :stylesheets do %>
<style type="text/css">
  #content {
    position: relative;
    padding: 10px;
  }
  span.has-failed-attempts, span.on-hold, span.slow {
    color: orange;
  }
  span.has-failed-max-attempts, span.super-slow {
    color: red;
  }
  dl#job-attributes dt {
    float:left;
    clear:left;
  }
  dl#job-attributes dd {
    padding-left: 2em;
    float:left;
  }
  #show-job {
    margin-left: 25px;
    float:left;
  }
  #show-job dt {
    font-weight: bold;
  }
  #show-job h4 {
    font-size: 14px;
  }
  h2 {
    font-size: 16px;
  }
  div.grid {
    border: 2px solid #ccc;
    margin-bottom: 10px;
  }
  #tags-wrapper h2 { float:left; }
  #tags-flavor {
    float:right;
  }
  #jobs-grid { float:left; margin-top: 6px; }
  .slick-row.ui-state-active {
    color: black;
    font-weight: normal;
  }
  .selected {
    background-color: #ddd;
  }
  .show-field-wrapper { display: none; }
  textarea.show-field { width: 850px; height: 630px; }
  .unloaded-id { color: #aaa; }
</style>
<% end %>
<% css_bundle :slickgrid %>
<% js_bundle :jobs %>

<div id="running-wrapper" style="float:left">
  <h2><%= t 'headings.running_jobs', "Running Jobs" %></h2>
  <div id="running-grid" style="width:700px; height:250px;" class='grid'>
  </div>
</div>
<div id="tags-wrapper" style="float:right">
  <h2><%= t 'headings.popular_tags', "Popular Tags" %></h2>
  <select id="tags-flavor">
    <%= options_for_select([[t('job_options.current', "Current"), 'current'],
                            [t('job_options.all', "All"), 'all']], @flavor) %>
  </select>
  <div id="tags-grid" style="clear:both; width:250px; height:250px" class='grid'>
  </div>
</div>
<div style="clear:both">
  <h2><%= t 'headings.jobs_list', "Jobs List" %></h2>
  <div id="jobs-grid-controls">
    <select id="jobs-flavor">
      <%= options_for_select([[t('job_options.current', "Current"), 'current'],
                              [t('job_options.future', "Future"), 'future'],
                              [t('job_options.failed', "Failed"), 'failed'],
                              [t('job_options.id', "ID"), 'id'],
                              [t('job_options.strand', "Strand"), 'strand'],
                              [t('job_options.tag', "Tag"), 'tag'],
      ], @flavor) %>
    </select>
    <input type='search' results='15' placeholder='<%= t :search_jobs, "Search Jobs" %>' autosave='canvas-jobs-search' id='jobs-search' name='q' value="<%= params[:q].presence %>" />
    <button id='jobs-refresh'><%= t '#buttons.refresh', "refresh" %></button>
    <%= before_label :total_jobs, "Total" %> <span id='jobs-total'><%= @jobs_count %></span>
    <% if Account.site_admin.grants_right?(@current_user, :manage_jobs) %>
      <button id='select-all-jobs'><%= t 'buttons.select_all', 'select all' %></button>
      <%= before_label :with_selection, "With Selection" %>
      <button id='hold-jobs'><%= t 'buttons.hold', 'hold' %></button>
      <button id='un-hold-jobs'><%= t 'buttons.un_hold', 'un-hold' %></button>
      <button id='delete-jobs'><%= t 'buttons.delete', 'delete' %></button>
    <% end %>
  </div>
  <div id="jobs-grid" style="width:700px; height:300px;" class='grid'>
  </div>
<div id="show-job">
  <h2><%= t 'headings.selected_job', "Selected Job" %></h2>
  <h4><%= before_label :id, "id" %> <a href="#" id="job-id-link"><span id="job-id" class="show-field">&nbsp;</span></a></h4>
  <h4><%= before_label :tag, "tag" %> <span id='job-tag' class='show-field'>&nbsp;</span></h4>
  <h4><%= before_label :strand, "strand" %> <span id='job-strand' class='show-field'>&nbsp;</span></h4>
  <table id='job-attributes' cellspacing="0">
    <tr>
      <td width="150"><%= before_label :priority, "Priority" %></td>
      <td id="job-priority" class="show-field">&nbsp;</td>
    </tr>
    <tr>
      <td><%= before_label :num_attempts, "Attempts" %></td>
      <td id="job-attempts" class="show-field">&nbsp;</td>
    </tr>
    <tr>
      <td><%= before_label :max_attempts, "Max Attempts" %></td>
      <td id="job-max_attempts" class="show-field">&nbsp;</td>
    </tr>
    <tr>
      <td><%= before_label :locked_by, "Locked By" %></td>
      <td id="job-locked_by" class="show-field">&nbsp;</td>
    </tr>
    <tr>
      <td><%= before_label :run_at, "Run At" %></td>
      <td id="job-run_at" class="show-field">&nbsp;</td>
    </tr>
    <tr>
      <td><%= before_label :locked_at, "Locked At" %></td>
      <td id="job-locked_at" class="show-field">&nbsp;</td>
    </tr>
    <% if defined?(Marginalia) %>
      <tr>
        <td><%= before_label :source, "Source" %></td>
        <td id="job-source" class="show-field">&nbsp;</td>
      </tr>
    <% end %>
    <tr>
      <td><%= before_label :failed_at, "Failed At" %></td>
      <td id="job-failed_at" class="show-field">&nbsp;</td>
    </tr>
    <tr>
      <td><%= before_label :original_job_id, "Original Job ID" %></td>
      <td id="job-original_job_id" class="show-field">&nbsp;</td>
    </tr>
    <tr>
      <td><%= before_label :handler, "Handler" %></td>
      <td><a href='#' id='job-handler-show'>(<%= t 'actions.show', "show" %>)</a>
        <div id='job-handler-wrapper' class='show-field-wrapper'><textarea id='job-handler' class='show-field'></textarea></div>
      </td>
    </tr>
    <tr>
      <td><%= before_label :last_error, "Last Error" %></td>
      <td><a href='#' id='job-last_error-show'>(<%= t 'actions.show', "show" %>)</a>
        <div id='job-last_error-wrapper' class='show-field-wrapper'><textarea id='job-last_error' class='show-field'></textarea></div>
      </td>
    </tr>
  </table>
</div>
</div>

<%
jobs_opts = {
  :max_attempts => Delayed::Settings.max_attempts,
  :flavor => @flavor,
  :url => jobs_url(:only => :jobs),
  :batch_update_url => batch_update_jobs_url,
  :job_url => jobs_url,
  :on_hold_attempt_count => Delayed::Job::ON_HOLD_COUNT,
  :limit => JobsController::LIMIT,
  :starting_query => params[:q],
  :slow_threshold => Setting.get('jobs.slow_threshold', 600).to_f,
  :super_slow_threshold => Setting.get('jobs.super_slow_threshold', 3600).to_f
}
running_opts = jobs_opts.merge({
  :url => jobs_url(:only => :running),
  :refresh_rate => @running_jobs_refresh_seconds * 1000,
  :starting_query => nil,
})
tags_opts = {
  :url => jobs_url(:only => :tags),
  :refresh_rate => @job_tags_refresh_seconds * 1000,
}

js_env :JOBS => {
  :opts => jobs_opts.as_json(:include_root => false),
  :running_opts => running_opts.as_json(:include_root => false),
  :tags_opts => tags_opts
}
%>

